//
//  ThirteenOneViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/13.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit
import Koloda

class ThirteenOneViewController: UIViewController {

    fileprivate lazy var mutDataArr:NSMutableArray = {
        var mutArr = NSMutableArray();
        mutArr.add("onepiece");
        mutArr.add("picture_01");
        mutArr.add("颜色值16进制表");
        mutArr.add("onepiece");
        mutArr.add("picture_01");
        mutArr.add("颜色值16进制表");
        return mutArr;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Koloda卡片";
        self.view.backgroundColor = .white;
        // Do any additional setup after loading the view.
        self.onCreateUIForView();
    }
    

    //MARK:UI
    fileprivate func onCreateUIForView() {
        let kolodaView = KolodaView(frame: CGRect(x: (screenMainSize.width-200)/2, y: (screenMainSize.height-200)/2, width: 200, height: 200));
        kolodaView.delegate = self;
        kolodaView.dataSource = self;
        self.view.addSubview(kolodaView);
    }

}

extension ThirteenOneViewController:KolodaViewDelegate {
    //图片划出去的方向
    func koloda(_ koloda: KolodaView, allowedDirectionsForIndex index: Int) -> [SwipeResultDirection] {
        return [.left,.up];
    }
    
    //是否允许图片划出去
    func koloda(_ koloda: KolodaView, shouldSwipeCardAt index: Int, in direction: SwipeResultDirection) -> Bool {
        return true;
    }
    
    //所有图片移除后响应
    func kolodaDidRunOutOfCards(_ koloda: KolodaView) {
        print("图片已移除");
    }
    
    func koloda(_ koloda: KolodaView, didSelectCardAt index: Int) {
        print("选择的第\(index)图片");
    }
    
    func kolodaShouldApplyAppearAnimation(_ koloda: KolodaView) -> Bool {
        return true;
    }
    
    func kolodaShouldMoveBackgroundCard(_ koloda: KolodaView) -> Bool {
        return true;
    }
    
    func kolodaShouldTransparentizeNextCard(_ koloda: KolodaView) -> Bool {
        return true;
    }
}

extension ThirteenOneViewController:KolodaViewDataSource {
    
    func kolodaNumberOfCards(_ koloda: KolodaView) -> Int {
        return self.mutDataArr.count;
    }
    
    func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
        let bgView = UIView(frame: koloda.bounds);
        bgView.backgroundColor = .green;
        
        let imgView = UIImageView(image: UIImage(named: self.mutDataArr[index] as! String));
        imgView.frame = koloda.bounds;
        imgView.backgroundColor = .lightGray;
        imgView.contentMode = .scaleToFill;
        bgView.addSubview(imgView);
        
        return bgView;
    }
    
    //速度级别
    func kolodaSpeedThatCardShouldDrag(_ koloda: KolodaView) -> DragSpeed {
        return .slow;
    }
    
    //图片划出去时显示在图片上的折叠层
    func koloda(_ koloda: KolodaView, viewForCardOverlayAt index: Int) -> OverlayView? {
        let overlayView = OverlayView(frame: koloda.bounds);
        overlayView.overlayState = .left;
        overlayView.backgroundColor = .orange;
        return overlayView;
    }
}
